package com.maibow.service.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import com.maibow.entity.admin.Module;
import com.maibow.entity.admin.OperPrivilege;
import com.maibow.entity.admin.SysOperator;
import com.maibow.util.CommonConstants;
import com.maibow.util.PageData;

@Service("commonService")
public class CommonService {

    /**
     * 
     * getCurrentOperator(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 返回值：SysOperator
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public SysOperator getCurrentOperator() {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        // shiro管理的session
        SysOperator operator = (SysOperator) session.getAttribute(CommonConstants.SESSION_OPERATOR);
        return operator;
    }

    /**
     * 
     * getOperPrivilege(这里用一句话描述这个方法的作用)
     * (这里描述这个方法适用条件 – 可选)
     * 标记：@return
     * 返回值：PageData
     * 作者：吴比(Wubi)
     * 
     * @exception
     * @since 1.0.0
     */
    public PageData getOperPrivilege(String path) {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        PageData pageData = new PageData();
        // 从SESSION里拿到操作权限
        List<Module> moduleList = (List) session.getAttribute(CommonConstants.SESSION_MODULE_LIST); // 获取菜单列表
        List<OperPrivilege> operPrivilegeList = null;
        for (Module module : moduleList) {
            if (module.getSubmodule() != null) {
                for (Module subModule : module.getSubmodule()) {
                    if (subModule.getModuleurl().equals(path)) {
                        operPrivilegeList = subModule.getOperprivileges();
                    }
                }
            }
        }

        for (OperPrivilege operPrivilege : operPrivilegeList) {
            // if
            // (operPrivilege.getOperUrl().equals(CommonConstants.OPER_PRIVILEGE_QUERY))
            // {
            // pageData.put(CommonConstants.OPER_PRIVILEGE_QUERY,
            // 1);
            // } else if
            // (operPrivilege.getOperUrl().equals(CommonConstants.OPER_PRIVILEGE_ADD))
            // {
            // pageData.put(CommonConstants.OPER_PRIVILEGE_ADD,
            // 1);
            // } else if
            // (operPrivilege.getOperUrl().equals(CommonConstants.OPER_PRIVILEGE_EDIT))
            // {
            // pageData.put(CommonConstants.OPER_PRIVILEGE_EDIT,
            // 1);
            // } else if
            // (operPrivilege.getOperUrl().equals(CommonConstants.OPER_PRIVILEGE_DELETE))
            // {
            // pageData.put(CommonConstants.OPER_PRIVILEGE_DELETE,
            // 1);
            // } else if
            // (operPrivilege.getOperUrl().equals(CommonConstants.OPER_PRIVILEGE_UPLOAD))
            // {
            // pageData.put(CommonConstants.OPER_PRIVILEGE_UPLOAD,
            // 1);
            // } else if
            // (operPrivilege.getOperUrl().equals(CommonConstants.OPER_PRIVILEGE_DOWNLOAD))
            // {
            // pageData.put(CommonConstants.OPER_PRIVILEGE_DOWNLOAD,
            // 1);
            // } else {
            //
            // }
            pageData.put(operPrivilege.getOperurl(), 1);
        }

        return pageData;
    }
}
